package cn.yuxiaoyuan.controller.app;

import cn.yuxiaoyuan.domain.OpenIdResult;
import cn.yuxiaoyuan.pojo.domain.DriverAuthMaterial;
import cn.yuxiaoyuan.pojo.ro.PointRo;
import cn.yuxiaoyuan.result.JSONResult;
import cn.yuxiaoyuan.service.IDriverAuthMaterialService;
import cn.yuxiaoyuan.service.IDriverService;
import cn.yuxiaoyuan.template.WechatLoginTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author solargen
 * @version 1.0
 * @date 2025/6/15 14:43
 */
@RestController
@RequestMapping("/wechat")
@RefreshScope
public class WechatController {
    @Autowired
    private IDriverService driverService;
    @Autowired
    private WechatLoginTemplate wechatLoginTemplate;
    @Autowired
    private IDriverAuthMaterialService driverAuthMaterialService;
    @GetMapping("/register/{code}/{phone}")
    public JSONResult register(@PathVariable("code") String code,@PathVariable("phone")String  phone){
        //拿到code去访问微信的服务器换取openid和session_key
        OpenIdResult openIdResult = wechatLoginTemplate.getWechatAccessTokenUrl(code);
        String openid = openIdResult.getOpenid();
        return  driverService.register(code,openid,phone);
    }

    @GetMapping("/driver/state")
    public JSONResult getDriverState(){
        return driverService.getDriverState();
    }

    @PostMapping("/driver/auth")
    public JSONResult driverAuth(@RequestBody DriverAuthMaterial driverAuthMaterial){
        return driverAuthMaterialService.driverAuth(driverAuthMaterial);
    }

    /**
     * 司机上线 不用管司机的坐标  只管将司机的配置加载到redis里面就可以了
     * @return
     */
    @PostMapping("/driver/online")
    public JSONResult driverOnline(){
        return driverService.driverOnline();
    }
    /**
     * 司机下线
     * @return
     */
    @PostMapping("/driver/offline")
    public JSONResult driverOffline(){
        return driverService.driverOffline();
    }

    @PostMapping("/location/point")
    public JSONResult locationPoint(@RequestBody @Valid PointRo point){
        return driverService.locationPoint(point);
    }

    @GetMapping("/pullOrderForDriver")
    public JSONResult pullOrderForDriver(){
        return driverService.pullOrderForDriver();
    }
}
